package model;

import java.util.Arrays;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.DuplicateKeyException;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import databeans.Employee;

/*
 * 
 * made by alesmana@andrew.cmu.edu
 * class for 15-600 homework 9
 * 
 * UserDAO
 * 
 */

public class EmployeeDAO {
    private BeanFactory<Employee> factory;

    
    public EmployeeDAO() throws DAOException {
        try {
            // Get a BeanTable so we can create the "employee" table
            BeanTable<Employee> employeeTable = BeanTable.getInstance(Employee.class, "cfa_employee");

            if (!employeeTable.exists()) {
                employeeTable.create("username"); //Employee's username will be the PK for the table

                employeeTable.setIdleConnectionCleanup(true); //see below explanation
                // Get a BeanFactory which the actions will use to read and
                // write rows of the "user" table
                factory = employeeTable.getFactory();
                
                //initially populate with sample data
                bootData();
            } else {
                // Long running web apps need to clean up idle database connections.
                // So we can tell each BeanTable to clean them up. (You would only notice
                // a problem after leaving your web app running for several hours.)
                employeeTable.setIdleConnectionCleanup(true);

                // Get a BeanFactory which the actions will use to read and
                // write rows of the "user" table
                factory = employeeTable.getFactory();
            }

        } catch (BeanFactoryException e) {
            throw new DAOException(e);
        }
    }
   

    private void bootData() throws DAOException {
        
        Employee employeeTest = new Employee("emp1");
        employeeTest.setFirstName("Atilla");
        employeeTest.setLastName("Hun");
        employeeTest.setPassword("emp1");
        employeeTest.setEmail("alesmana+atilla@gmail.com");
        create(employeeTest);
        
        
        employeeTest = new Employee("emp2");
        employeeTest.setFirstName("Gengis");
        employeeTest.setLastName("Khan");
        employeeTest.setPassword("emp2");
        employeeTest.setEmail("alesmana+khan@gmail.com");
        create(employeeTest);
        
        
        
    }

    public void create(Employee employee) throws DAOException {
        try {
            Transaction.begin();
            Employee dbEmployee = factory.create(employee.getUsername());
            factory.copyInto(employee, dbEmployee);
            Transaction.commit();
        } catch (DuplicateKeyException e) {
            throw new DAOException("A user named " + employee.getUsername() + " has already exists");
        } catch (RollbackException e) {
            throw new DAOException(e);
        } finally {
            if (Transaction.isActive())
                Transaction.rollback();
        }
    }

    public Employee lookup(String username) throws DAOException {
        try {
            return factory.lookup(username);
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
    }

    protected BeanFactory<Employee> getFactory() {
        return factory;
    }

    public Employee[] getEmployees() throws DAOException {
        try {
            Employee[] employees = factory.match();
            Arrays.sort(employees); // We want them sorted by last and first names (as per X.compareTo());
            return employees;
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
    }

    public void setPassword(String username, String password)
            throws DAOException {
        try {
            Transaction.begin();
            Employee dbEmployee = factory.lookup(username);
            if (dbEmployee == null) {
                throw new DAOException("Employee " + username + " no longer exists");
            }
            dbEmployee.setPassword(password);
            Transaction.commit();
        } catch (RollbackException e) {
            throw new DAOException(e);
        } finally {
            if (Transaction.isActive())
                Transaction.rollback();
        }
    }
}
